﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene;
using Lucene.Net.Index;
using System.IO;
using Lucene.Net.Store;
using PanGu;
using Lucene.Net.Analysis.PanGu;
namespace LuceneNetServices
{
    /// <summary>
    /// 获取IndexWriter的唯一实例
    /// </summary>
    public class SingleIndexWriterFactory
    {
        private static IndexWriter _indexWriter;
        private static string _strDirectory;

        public static IndexWriter GetSingleWriter()
        {

            if (_indexWriter == null || string.IsNullOrEmpty(_strDirectory))
            {
                _strDirectory = System.Configuration.ConfigurationManager.AppSettings["indexDirectory"];
                if (string.IsNullOrEmpty(_strDirectory))
                {
                    throw new Exception("索引路径不存在");
                }
                System.IO.DirectoryInfo directoryInfo = new DirectoryInfo(_strDirectory);
                if (System.IO.Directory.Exists(_strDirectory))
                {
                    directoryInfo = System.IO.Directory.CreateDirectory(_strDirectory);
                }
                Lucene.Net.Store.Directory indexDirectory = FSDirectory.Open(directoryInfo);
                PanGuAnalyzer panguAnalyzer = new PanGuAnalyzer();
                _indexWriter = new IndexWriter(indexDirectory, panguAnalyzer, IndexWriter.MaxFieldLength.LIMITED);
            }
            return _indexWriter;
        }
    }
}
